<?php

class __mail_comments extends Module {
	
	public function __construct () {
		parent::__construct ('mail');
	}
	
	public function getcomments ($messageId = false) {
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance ();
		
		$oCommentType = $typesController->getTypeByName ('message_reply');
		$oTypeMessage = $typesController->getTypeByName ('message');
		
		$sel = new Selection;
		$sel->addType ($oCommentType);
		$sel->setOrder ('pubdate');
		
		if (!$messageId) {
			$messageId = getRequest ('param0','int');
		}
		
		if (!$oMessage = $objectsController->getObject ($oTypeMessage, $messageId)) {
			throw new NotFound;
		}
		
		$userId = ExplaySession::getUserId ();
		
		if ($userId != $oMessage->getValue ('sender_id') && $userId != $oMessage->getValue ('addressed_user_id')) {
			throw new NotFound;
		}
		
		$sel->addFilter ('object_id', $messageId);
		
		$aObjects = $objectsController->getObjectsList ($sel->getList ());
		
		$aRelatedList = array ();
		$aNormalList = array ();
		
		foreach ($aObjects as $object) {
			$aRelatedList[$object->getValue('rel_id')][] = $object;
		}
		
		if (isset ($aRelatedList[0])) {
			$this->detectLevel ($aRelatedList[0], 0, $aRelatedList, $aNormalList);
		}
		
		$response = new ModuleResponse;
		$response->setTemplate (getRequest('reload', 'bool') ? 'mail/showComments' : 'mail/commentsList');
		$response->setAttribute ('object-id', $messageId);
		
		foreach ($aNormalList as $object) {
			$response->addObject ($object);
		}
		
		return $response;
	}
	
	/**
	 * Добавляет комментарий к сообщению
	 * @return void
	 */
	public function ajax_addcomment () {
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance ();
		
		$oType = $typesController->getTypeByName ('message');
		$oCommentType = $typesController->getTypeByName ('message_reply');
		
		$requestObjectId = getRequest ('param0', 'int');
		
		if (!$oMessage = $objectsController->getObject ($oType, $requestObjectId)) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml'); 
		}
		
		$userId = ExplaySession::getUserId ();
		
		if ($userId != $oMessage->getValue ('sender_id') && $userId != $oMessage->getValue ('addressed_user_id')) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml'); 
		}
		
		$oComment = new Object ($oCommentType);
		$oComment->setValues ($this->getRequestData ('new', $oCommentType, true));
		$oComment->setValue ('owner_id', ExplaySession::getUserId ());
		$oComment->setValue ('pubdate', time ());
		$oComment->setValue ('object_id', $requestObjectId);
		
		if (count ($this->getValidateErrors ($oComment, $oCommentType)) > 0) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml'); 
		}
		
		if ($oComment->getValue ('rel_id') != 0 && !$oRelComment = $objectsController->getObject ($oCommentType, $oComment->getValue ('rel_id'))) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml'); 
		}
		
		$oComment->createNew ();
		
		$countComments = $oMessage->getValue ('count_comments');
		$oMessage->setValue ('count_comments', ++$countComments);
		$oMessage->setValue ('date', time ());
		$oMessage->save ();
		
		if ($oComment->getValue ('owner_id') == $oMessage->getValue ('sender_id')) {
			$addressedUId = $oMessage->getValue ('addressed_user_id');
		} elseif ($oComment->getValue ('owner_id') == $oMessage->getValue ('addressed_user_id')) {
			$addressedUId = $oMessage->getValue ('sender_id');
		}
		
		$oTypeStatus = $typesController->getTypeByName ('message_status');
		
		$readSel = new Selection;
		$readSel->addType ($oTypeStatus);
		$readSel->addFilter ('message_id', $requestObjectId);
		$readSel->addFilter ('type_id', $oType->getId ());
		$readSel->addFilter ('user_id', $addressedUId);
		$readSel->addFilter ('read', '1');
		
		$res = $readSel->getList ();

		if (count ($res)) {
			$oRead = $objectsController->getObject ($oTypeStatus, $res[0][0]);
			$oRead->setValue ('read', '0');
			$oRead->save ();
		}
		
		$this->messageReplyNotification ($oMessage, $oComment);
		
		$this->flush ((string) new Array2XML ('data', array ('result' => '1')), 'text/xml');
	}
	
	
	private function detectLevel (array &$list, $level, &$aRelatedList, &$aNormalList) {
		foreach ($list as $oComment) {
			$oComment->setAttribute ('comment-level', $level);
			$aNormalList[] = $oComment;
			if (isset ($aRelatedList[$oComment->getId()])) {
				$this->detectLevel ($aRelatedList[$oComment->getId()], $level+1, $aRelatedList, $aNormalList);
			}
		}
	}
	
}